#include "weapon.h"
#include "game.h"
#include <unistd.h>
#include <string.h>

s_back      *g_back;
s_player    *g_player_1;
char        **g_skin_list;
s_wlist     *g_wlist = NULL;
/*
** Copy a weapon from the general table.
*/

s_weapon      **init(void)
{
    s_weapon      **tab = NULL;
        tab = malloc(sizeof(s_weapon) * 5);
           for (int j = 0; j < 5; j++)
                tab[j] = malloc(sizeof (s_weapon));
    tab[0]->damage = 50;
    tab[0]->speed = 0;
    tab[0]->size.x = 13;
    tab[0]->size.y = 16;
    tab[0]->coord.x = 0;
    tab[0]->coord.y = 0;
    tab[0]->ally = 0;
    tab[0]->alive = 0;
    tab[0]->skin = IMG_Load(g_skin_list[0]);
    tab[1]->damage = 70;
    tab[1]->speed = 0;
    tab[1]->size.x = 23;
    tab[1]->size.y = 16;
    tab[1]->coord.x = 0;
    tab[1]->coord.y = 0;
    tab[1]->ally = 0;
    tab[1]->alive = 0;
    tab[1]->skin = IMG_Load(g_skin_list[1]);
    tab[2]->damage = 90;
    tab[2]->speed = 0;
    tab[2]->size.x = 12;
    tab[2]->size.y = 14;
    tab[2]->coord.x = 0;
    tab[2]->coord.y = 0;
    tab[2]->ally = 0;
    tab[2]->alive = 0;
    tab[2]->skin = IMG_Load(g_skin_list[2]);
    tab[3]->damage = 120;
    tab[3]->speed = 0;
    tab[3]->size.x = 11;
    tab[3]->size.y = 43;
    tab[3]->coord.x = 0;
    tab[3]->coord.y = 0;
    tab[3]->ally = 0;
    tab[3]->alive = 0;
    tab[3]->skin = IMG_Load(g_skin_list[3]);
    return (tab);
}

char            **init_skin_list(void)
{
    char        **skin = NULL;

    skin = malloc(sizeof(char *) * 5);
        for(int i = 0; i < 4; i++)
            skin[i] = malloc(sizeof (char) * 40);
    skin[0] = strcpy(skin[0], "src/picture/missile1.png");
    skin[1] = strcpy(skin[1], "src/picture/missile2.png");
    skin[2] = strcpy(skin[2], "src/picture/shuriken.png");
    skin[3] = strcpy(skin[3], "src/picture/laser.png");
    return (skin);
}

s_weapon        *init_weapon(s_weapon *weapon)
{
  s_weapon  *w = malloc(sizeof (s_weapon));

  w->damage = weapon->damage;
  w->coord.x = 0;
  w->coord.y = 0;
  w->size.x = weapon->size.x;
  w->size.y = weapon->size.y;
  w->speed = weapon->speed;
  w->skin = weapon->skin;
  w->alive = 1;

  return (w);
}

void                shoot(s_weapon      *weap,
                            SDL_Surface   **screen)
{
    s_weapon    *w = NULL;

    w = init_weapon(weap);

    w->coord.x = g_player_1->coord.x + 20;
    w->coord.y = g_player_1->coord.y;
    w->alive = 1;
    w->ally = 1;
    draw_rect(w->coord.x, w->coord.y, w->skin, *screen);
      g_wlist  = add_missile(w, g_wlist);

}

void        update_wlist(s_wlist        *wl,
                         SDL_Surface    **screen)
{
    s_wlist *l = NULL;
    int     cpt = 0;

    if ((wl == NULL))
        return;
    else
    {
        l = wl;

        while (l != NULL)
        {
            if ((l->weapon->coord.y > 1) && (l->weapon->alive == 1))
            {
                l->weapon->coord.y = l->weapon->coord.y - 3;
                draw_rect(l->weapon->coord.x, l->weapon->coord.y, l->weapon->skin, *screen);
            }
            else
                l->weapon->alive = 0;
            cpt++;
            l = l->next;
        }
    }

}
